// 文件加载完后,在加载js
window.addEventListener("load", function () {
	// 跳转
	$("header a").addEventListener("click", function () {
		window.location = "../html/log.html";
	});
	// 登录注册
	// 1.用户名部分
	$("#usename").addEventListener("blur", verifyName);
	function verifyName() {
		const ss = /^([a-zA-Z0-9]){6,20}$/;
		if (!ss.test($("#usename").value)) {
			$("#usename").nextElementSibling.style.display = "block";
			return false;
		}
		$("#usename").nextElementSibling.style.display = "none";
		return true;
	}

	// 2.手机号
	$("#tell").addEventListener("blur", verifyTell);
	function verifyTell() {
		const ss = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
		if (!ss.test($("#tell").value)) {
			$("#tell").nextElementSibling.style.display = "block";
			return false;
		}
		$("#tell").nextElementSibling.style.display = "none";
		return true;
	}
	// 3.密码
	$("#psw").addEventListener("blur", verifyPsw);
	function verifyPsw() {
		const ss = /^([a-zA-Z0-9]){6,12}$/;
		if (!ss.test($("#psw").value)) {
			$("#psw").nextElementSibling.style.display = "block";
			return false;
		}
		$("#psw").nextElementSibling.style.display = "none";
		return true;
	}
	// 4.验证码
	let flag = true;
	$("#enroll .incept").addEventListener("click", function () {
		if (flag) {
			flag = false;
			this.innerHTML = "距离下次验证还剩10秒";
			this.style = `font-size:12px; background-color: #ccc; color:white;`;
			let time = 10;
			let timer = setInterval(function () {
				time--;
				$("#enroll .incept").innerHTML = `距离下次验证还剩${time}秒`;
				if (time === 0) {
					clearInterval(timer);
					$("#enroll .incept").style = "";
					flag = true;
					$("#enroll .incept").innerHTML = `获取验证码`;
				}
			}, 1000);
		}
	});
	// 提交
	$("#enroll").addEventListener("submit", function (e) {
		if (!$("#check").checked) {
			alert("请勾选同意协议");
			e.preventDefault();
		}
		if (!verifyName()) e.preventDefault();
		if (!verifyTell()) e.preventDefault();
		if (!verifyPsw()) e.preventDefault();
	});
});
